package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.domain.CustomerTransferHistory;
import cn.congzhou.crm.domain.Employee;
import cn.congzhou.crm.mapper.CustomerMapper;
import cn.congzhou.crm.mapper.CustomerTransferHistoryMapper;
import cn.congzhou.crm.query.QueryObject;
import cn.congzhou.crm.service.ICustomerTransferHistoryService;
import cn.congzhou.crm.utils.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerTransferHistoryServiceImpl implements ICustomerTransferHistoryService {

    @Autowired
    private CustomerTransferHistoryMapper customerTransferHistoryMapper;

    @Autowired
    private CustomerMapper customerMapper;

    //    保存跟进记录
    @Override
    public void saveOrUpdate(CustomerTransferHistory entity) {
        Employee emp = UserContext.getCurrentUser();
        entity.setOperateTime(new Date());
        entity.setOperator(emp);
        customerTransferHistoryMapper.insert(entity);
//      修改客户状态和更新营销人员
        customerMapper.updateTransferById(entity.getNewSeller().getId(),0,entity.getCustomer().getId());

    }

    //    分页查询数据
    @Override
    public PageInfo<CustomerTransferHistory> selectByPage(QueryObject qo) {
//      调用这个类的静态方法处理分页数据
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "his.operate_time DESC");
        List<CustomerTransferHistory> list = customerTransferHistoryMapper.selectByPage(qo);
//      创建页面信息对象封装起来
        PageInfo<CustomerTransferHistory> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
